package Server;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;

import packages.Packages;

public class Game {

	private ArrayList<String> wordList;
	private String currentWord;
	private ConnectedClient currentDrawer;
	private ArrayList<ConnectedClient> clients;
	Server server;

	private boolean gameStarted;

	public Game(Server server, ArrayList<ConnectedClient> clients) {
		this.server = server;
		this.clients = clients;
		currentWord = "";
		currentDrawer = null;
		gameStarted = false;
		wordList = new ArrayList<String>();

	}

	public synchronized void setupNewGame(ConnectedClient winner) {
		if (clients.isEmpty()) {
			return;
		}
		if (winner == null) {
			currentDrawer = clients.get(0);
		} else {
			currentDrawer = winner;
		}
		getNextWord();

		currentDrawer.send(currentWord + "\n", Packages.PACKAGE_WORD_TO_DRAWER);
	}

	public synchronized void newGame(boolean wordAccepted) {
		if (wordAccepted) {
			gameStarted = true;

			sendToAllButPainter("" + currentWord.length() + "\n",
					Packages.PACKAGE_LENGHT_OF_WORD);
			sendToAll("", Packages.PACKAGE_NEW_GAME);
			updateClientList();
		} else {
			getNextWord();
			currentDrawer.send(currentWord + "\n",
					Packages.PACKAGE_WORD_TO_DRAWER);
		}

	}

	public void getNextWord() {
		if (wordList.isEmpty()) {
			fillWordList();
		}
		String word = "";
		Random r = new Random();

		int next = r.nextInt(wordList.size());
		word = wordList.remove(next);
		currentWord = word;

	}

	public void fillWordList() {
		BufferedReader br = null;
		try {
			br = new BufferedReader(new FileReader("words.txt"));

			String line = br.readLine();

			while (line != null) {
				wordList.add(line);
				line = br.readLine();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			br.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void sendToAllButPainter(String mess, byte packageType) {
		for (ConnectedClient c : clients) {
			if (c != currentDrawer) {
				c.send(mess, packageType);
			}
		}
	}

	public boolean isDrawer(ConnectedClient c) {
		return (c == currentDrawer);
	}

	public void disconnectClient(ConnectedClient c) {
		if (isDrawer(c)) {
			currentDrawer = null;
		}
		clients.remove(c);
		updateClientList();
	}

	public boolean checkGuess(String guess) {
		return (guess.trim().toLowerCase().equals(currentWord));
	}

	public void endGame(String winnerName) {
		String message = winnerName + "," + currentWord + "\n";
		server.sendToAll(message, Packages.PACKAGE_ROUND_OVER);

	}

	public void sendToAll(String mess, byte packageType) {
		server.sendToAll(mess, packageType);
	}

	public void sendClientList() {
		String list = "";
		if (currentDrawer != null) {
			list = currentDrawer.getPlayerName() + ",";
		}

		for (ConnectedClient c : clients) {
			if (!c.equals(currentDrawer)) {
				list += c.getPlayerName() + ",";
			}
		}
		list += "\n";
		server.sendToAll(list, Packages.PACKAGE_CLIENT_LIST);
	}

	public void updateClientList() {
		for (ConnectedClient c : clients) {
			if (!c.isConnected()) {
				clients.remove(c);
			}
		}
		sendClientList();
	}

	public boolean isStarted() {
		return gameStarted;
	}
}
